lesson modified from Data Carpentry R-ecology-lesson
Here is an easy visualization on how ggplot layers on and how the different commands using this flipbook
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.0 ✔ purrr 0.3.2
## ✔ tibble 2.0.1 ✔ dplyr 0.7.8
## ✔ tidyr 0.8.2 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.3.0
## Warning: package 'tibble' was built under R version 3.5.2
## Warning: package 'purrr' was built under R version 3.5.2
## Warning: package 'stringr' was built under R version 3.5.2
## ── Conflicts ────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
# Download the data
download.file(url="https://ndownloader.figshare.com/files/2292169",
destfile = "portal_data_joined.csv")
surveys_complete <- read_csv("data/portal_data_joined.csv")
## Parsed with column specification:
## cols(
## record_id = col_double(),
## month = col_double(),
## day = col_double(),
## year = col_double(),
## plot_id = col_double(),
## species_id = col_character(),
## sex = col_character(),
## hindfoot_length = col_double(),
## weight = col_double(),
## genus = col_character(),
## species = col_character(),
## taxa = col_character(),
## plot_type = col_character()
## )
#view the first few rows
head(surveys_complete)
## # A tibble: 6 x 13
## record_id month day year plot_id species_id sex hindfoot_length
## <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <dbl>
## 1 1 7 16 1977 2 NL M 32
## 2 72 8 19 1977 2 NL M 31
## 3 224 9 13 1977 2 NL <NA> NA
## 4 266 10 16 1977 2 NL <NA> NA
## 5 349 11 12 1977 2 NL <NA> NA
## 6 363 11 12 1977 2 NL <NA> NA
## # … with 5 more variables: weight <dbl>, genus <chr>, species <chr>,
## # taxa <chr>, plot_type <chr>
a basic ggplot call is made up of: 1. ggplot() 2. geom_[some kind of plot]() 3. .. and more calls to make your plots more fancy or more complicated facets
where: 1. ggplot() –> holds your data that will be applied to all subsequent layers 2. geom_[some kind of plot]() –> determines the type of graph you have ie geom_line = line graph 3. and these calls are chained together by +
test_plot <- ggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length)) +
geom_hex()
pro tip: don’t forget that the + needs to be at the end of each line
a way to look at data more easily in an interactive plot - ggplot plot can be directly converted to a plotly plot
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
ggplotly()
## Warning: Removed 4048 rows containing non-finite values (stat_binhex).
ggplotly(test_plot)
## Warning: Removed 4048 rows containing non-finite values (stat_binhex).
last_plot()
ggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length)) +
geom_point()
## Warning: Removed 4048 rows containing missing values (geom_point).
x, y and colour in aes()alpha and size are applied to the general points so are outside aes()labs() to make your axis and title look niceggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length, colour = species)) +
geom_point(alpha = 0.5, size = 10) +
geom_smooth(method = "lm", aes(group = 1)) +
labs(title = "new title here \nhello",
y = expression(paste("hindfoot length (cm^2)", mu, ")")),
x = "weight (g)",
colour = "rats")
## Warning: Removed 4048 rows containing non-finite values (stat_smooth).
## Warning: Removed 4048 rows containing missing values (geom_point).
library(ghibli)
## Warning: package 'ghibli' was built under R version 3.5.2
ghibli_palettes
## $MarnieLight1
## [1] "#95918E" "#2B2522" "#7BC7C8" "#8BBBD0" "#E4D0C4" "#B0DDEA" "#F4E8CF"
##
## $MarnieMedium1
## [1] "#7BA46C" "#602D31" "#008D91" "#0A789F" "#C6A28A" "#61B8D3" "#EACF9E"
##
## $MarnieDark1
## [1] "#15110F" "#301619" "#004849" "#083C50" "#645145" "#305E6A" "#72694E"
##
## $MarnieLight2
## [1] "#8E938D" "#93A39C" "#95B8AF" "#9FD1BE" "#BFCDBD" "#A9D1A7" "#E6E496"
##
## $MarnieMedium2
## [1] "#1C271D" "#254639" "#257161" "#3AA580" "#7F9A7C" "#52A356" "#CEC747"
##
## $MarnieDark2
## [1] "#0E130E" "#13231D" "#143830" "#1E513F" "#404D3E" "#29522A" "#666421"
##
## $PonyoLight
## [1] "#A6A0A0" "#ADB7BF" "#91C5CB" "#F7ACB3" "#F0BBB2" "#EDD7A4" "#F6E2D5"
##
## $PonyoMedium
## [1] "#4D4140" "#596F7E" "#168B98" "#ED5B67" "#E27766" "#DAAD50" "#EAC3A6"
##
## $PonyoDark
## [1] "#272020" "#2D3840" "#0E464C" "#762D34" "#703C33" "#6D5826" "#766255"
##
## $LaputaLight
## [1] "#898D90" "#8D939F" "#9F99B2" "#AFACC6" "#D8CADC" "#D9EDF2" "#F8E9C2"
##
## $LaputaMedium
## [1] "#13191F" "#1F2945" "#413566" "#5D5A8E" "#AF93BA" "#B2DAE4" "#F1D687"
##
## $LaputaDark
## [1] "#080D0F" "#0D1320" "#201A34" "#302D47" "#584B5D" "#596D72" "#786A43"
##
## $MononokeLight
## [1] "#838A8F" "#BB958B" "#9EA7BC" "#B2B8B1" "#EAA69D" "#F4C59D" "#F5EDCD"
##
## $MononokeMedium
## [1] "#05141E" "#762B19" "#3D507A" "#657062" "#D14E3E" "#E78A40" "#EBD799"
##
## $MononokeDark
## [1] "#020A0F" "#3C150C" "#1F283D" "#333832" "#69261E" "#74461F" "#756D4D"
##
## $SpiritedLight
## [1] "#8F9296" "#9A9C97" "#C29A9B" "#C7C0C7" "#B1DDF3" "#E1D7CC" "#DAEBF7"
##
## $SpiritedMedium
## [1] "#1F272E" "#353832" "#853438" "#908091" "#61BAE5" "#C4AE99" "#B5D9F0"
##
## $SpiritedDark
## [1] "#0F1216" "#1A1C17" "#431A1C" "#484148" "#325D73" "#62574D" "#5A6B77"
##
## $YesterdayLight
## [1] "#758184" "#7D8C96" "#87988E" "#9CAFC1" "#AFD5BD" "#EDE096" "#C1DAE8"
##
## $YesterdayMedium
## [1] "#041A21" "#102F40" "#244331" "#4B6E90" "#6AB287" "#DDC850" "#8FBBD6"
##
## $YesterdayDark
## [1] "#020E11" "#0A1923" "#14251B" "#293D4F" "#3C624B" "#796E2B" "#4F6875"
##
## $KikiLight
## [1] "#8E8C8F" "#9A9AA1" "#D98594" "#82C3D8" "#D1C1AC" "#BEDDE0" "#EADBD1"
##
## $KikiMedium
## [1] "#1C1A1F" "#333544" "#B9022C" "#0085B0" "#9F825C" "#7EBAC2" "#D2B7A1"
##
## $KikiDark
## [1] "#0E0C0F" "#1A1A22" "#5B0315" "#004358" "#51412D" "#3E5D60" "#6A5B51"
##
## $TotoroLight
## [1] "#85898A" "#959492" "#AC9D96" "#A8A6A9" "#A1B1C8" "#D6C0A9" "#DCD3C4"
##
## $TotoroMedium
## [1] "#1C1A1F" "#2D2A25" "#593B2D" "#534C53" "#42668D" "#AF8058" "#BCA78F"
##
## $TotoroDark
## [1] "#05090A" "#151412" "#2C1D16" "#282629" "#213148" "#57402C" "#5C5346"
# display palettes w/ names
par(mfrow=c(9,3))
for(i in names(ghibli_palettes)) print(ghibli_palette(i))
pal <- ghibli_palette("KikiLight")
pal2 <- c(pal[3], pal[4])
scale_fill_ghibli_c (continuous) or _d (discrete)ggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length)) +
geom_hex() +
scale_fill_ghibli_c("PonyoLight")
## Warning: Removed 4048 rows containing non-finite values (stat_binhex).
https://github.com/karthik/wesanderson - this palette doesn’t have it’s own function yet
library(wesanderson)
# look at your palettes
wes_palettes
## $BottleRocket1
## [1] "#A42820" "#5F5647" "#9B110E" "#3F5151" "#4E2A1E" "#550307" "#0C1707"
##
## $BottleRocket2
## [1] "#FAD510" "#CB2314" "#273046" "#354823" "#1E1E1E"
##
## $Rushmore1
## [1] "#E1BD6D" "#EABE94" "#0B775E" "#35274A" "#F2300F"
##
## $Rushmore
## [1] "#E1BD6D" "#EABE94" "#0B775E" "#35274A" "#F2300F"
##
## $Royal1
## [1] "#899DA4" "#C93312" "#FAEFD1" "#DC863B"
##
## $Royal2
## [1] "#9A8822" "#F5CDB4" "#F8AFA8" "#FDDDA0" "#74A089"
##
## $Zissou1
## [1] "#3B9AB2" "#78B7C5" "#EBCC2A" "#E1AF00" "#F21A00"
##
## $Darjeeling1
## [1] "#FF0000" "#00A08A" "#F2AD00" "#F98400" "#5BBCD6"
##
## $Darjeeling2
## [1] "#ECCBAE" "#046C9A" "#D69C4E" "#ABDDDE" "#000000"
##
## $Chevalier1
## [1] "#446455" "#FDD262" "#D3DDDC" "#C7B19C"
##
## $FantasticFox1
## [1] "#DD8D29" "#E2D200" "#46ACC8" "#E58601" "#B40F20"
##
## $Moonrise1
## [1] "#F3DF6C" "#CEAB07" "#D5D5D3" "#24281A"
##
## $Moonrise2
## [1] "#798E87" "#C27D38" "#CCC591" "#29211F"
##
## $Moonrise3
## [1] "#85D4E3" "#F4B5BD" "#9C964A" "#CDC08C" "#FAD77B"
##
## $Cavalcanti1
## [1] "#D8B70A" "#02401B" "#A2A475" "#81A88D" "#972D15"
##
## $GrandBudapest1
## [1] "#F1BB7B" "#FD6467" "#5B1A18" "#D67236"
##
## $GrandBudapest2
## [1] "#E6A0C4" "#C6CDF7" "#D8A499" "#7294D4"
##
## $IsleofDogs1
## [1] "#9986A5" "#79402E" "#CCBA72" "#0F0D0E" "#D9D0D3" "#8D8680"
##
## $IsleofDogs2
## [1] "#EAD3BF" "#AA9486" "#B6854D" "#39312F" "#1C1718"
# assign the palette you want to use to something
pal <- wes_palette("Zissou1", 100, type = "continuous")
ggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length)) +
geom_hex() +
scale_fill_gradientn(colours = pal)
## Warning: Removed 4048 rows containing non-finite values (stat_binhex).
https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html - a nice package with colours that are colourblind friendly - this is similar to how ghibli works but doesn’t have a _c or _d function but instead uses either discrete = TRUE or FALSE
yearly_counts <- surveys_complete %>%
count(year, species_id)
library(viridis)
## Loading required package: viridisLite
ggplot(data = yearly_counts, mapping = aes(x = year, y = n, color = species_id)) +
geom_line()
ggplot(data = yearly_counts, mapping = aes(x = year, y = n, color = species_id)) +
geom_line() +
# add this line to use the viridis colour package
# TRUE for discrete variable and FALSE for continuous variables
scale_color_viridis(discrete = TRUE)